#include "StdAfx.h"
#include "ToListTreeItem.h"

#include "GTDTreeCtrl.h"
#include "../core/guid.h"
#include "../core/GtdDOMItem.h"
#include "mxgtd.h"
#include "resdef.h"

#include "../core/GtdDOMItem.h"
enum ItemDataType{
	_UNPROJ_TASKROOT,
	_PROJ_TASKROOT,
	_TASK,
};

class ToDoListTreeItemData : public TreeItemImpl
{
public:	
	ToDoListTreeItemData(ItemDataType t, const CGuid& id){   mt = t, gid = id; }
	ToDoListTreeItemData(ItemDataType t){ mt = t; }
	ItemDataType mt;
	CGuid gid;
};


void TodoListTreeItem::LoadTree(const CGuid& context, CGTDTreeCtrl& ctrl )
{
	ctrl.DeleteAllItems();
	GtdDOM& dom = theApp.getGTDDOM();
	GuidList itemlist =  dom.GetContextTaskItemList(context);
	for(GuidList::const_iterator itr = itemlist.begin();itr !=itemlist.end();++itr)
	{
		if(GtdDOMItem* pItem = dom.getItem(*itr))
		{
			if(GtdTaskItem* pTaskItme = pItem->toTaskItem())
			{
				AddTaskItemToTree(pTaskItme,ctrl);
			}
		}
	}

	

}

void TodoListTreeItem::LoadUnContextTree( CGTDTreeCtrl& ctrl )
{
	ctrl.DeleteAllItems();
	GtdDOM& dom = theApp.getGTDDOM();
	GuidList itemlist =  dom.GetUnContextTaskItemList();
	for(GuidList::const_iterator itr = itemlist.begin();itr !=itemlist.end();++itr)
	{
		if(GtdDOMItem* pItem = dom.getItem(*itr))
		{
			if(GtdTaskItem* pTaskItme = pItem->toTaskItem())
			{
				AddTaskItemToTree(pTaskItme,ctrl);
			}
		}
	}
}

TodoListTreeItem TodoListTreeItem::AddProjectToTree( GtdProject* pProj, CGTDTreeCtrl& ctrl )
{
	TodoListTreeItem root =  CTreeCtrlItem::GetRoot(ctrl);
	if(GtdProject* pParent = pProj->GetParent())
	{
		CTreeCtrlItem parentItem = root.findItem(pParent->m_id);
		if(!parentItem)
		{
			parentItem =  AddProjectToTree(pParent,ctrl);
		}
		
		TodoListTreeItem addItem = parentItem.AddChild(pProj->msName);
		return addItem; 
	}
	return root.AddChild(pProj->msName);
}

TodoListTreeItem TodoListTreeItem::findItem( const CGuid&guid ) const
{
	if(guid.isNull())
		return CTreeCtrlItem();

	CTreeCtrlItem end = GetPreOrderEnd();
	CTreeCtrlItem next  = *this;
	while(next!=end)
	{
		if(ToDoListTreeItemData* pImpl = (ToDoListTreeItemData*)next.GetImpl() )
		{
			if(pImpl->gid == guid)
				return next;
		}
		next = next.GetPreOrderNext();
	}
	return CTreeCtrlItem();
}

TodoListTreeItem TodoListTreeItem::AddTaskItemToTree( GtdTaskItem* pItem, CGTDTreeCtrl& ctrl )
{
	TodoListTreeItem root = CTreeCtrlItem::GetRoot(ctrl);
	TodoListTreeItem taskItem = root.findItem(pItem->m_id);
	if(!taskItem)
	{
		taskItem = root.AddChild(pItem->msName);
	}
	
	
	taskItem.RemoveAllChild();
	
	

	bool bAllDone = true;
	bool bAllUnDone = true;

	for(int i=0;i<pItem->GetCheckCount();i++)
	{
		TaskItemCheckItem* pcheckItem = pItem->GetCheckItem(i);
		TodoListTreeItem checkItem = taskItem.AddChild(pcheckItem->msName);
		//checkItem.SetImage(IMG_TASK,IMG_TASK);
		checkItem.SetState(pcheckItem->m_bDone?STATE_CHECK:STATE_UNCHECK);

		if(pcheckItem->m_bDone){ bAllUnDone = false; }
		else{ bAllDone  = false; }
	}

	int iState;
	if(bAllDone&&bAllUnDone)
	{
		iState = pItem->m_bDone;
	}
	else if(bAllUnDone!=bAllDone)
	{
		iState = bAllDone?STATE_CHECK:STATE_UNCHECK;
	}
	else
	{
		iState = STATE_HALFCHECK;
	}

	taskItem.SetState(iState);
	taskItem.Expand();

	return taskItem;
	
}


